package com.xiaohong.security.handle;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : xiaohong
 * @date : 2021/4/29 18:14
 * 自定义登录成功跳转处理器
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private String url;

    public MyAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        // 打印当前ip
        System.out.println("当前ip"+request.getRemoteAddr());

        // 获取User对象
        User user = (User)authentication.getPrincipal();
        // 获取用户信息
        System.out.println(user.getUsername());
        System.out.println(user.getPassword()); // 为保证安全，这里打印为null
        System.out.println(user.getAuthorities());

        //重定向成功页面
        response.sendRedirect(url);

    }

}
